<?php if ( ! defined('IN_DiliCMS')) exit('No direct script access allowed');
	
	class Acl  {
        
        /**
         * CI实例
         * 
         * @var object 
         */
		var $ci;
        
        /**
         * 顶部菜单项
         * 
         * @var array 
         */
		var $top_menus = array();
        
        /**
         * 左部菜单项
         * 
         * @var array 
         */
		var $left_menus = array();
        
        /**
         * 当前主菜单（上部模块区域)
         * 
         * @var int 
         */
		var $_current_menu = -1;
        
        /**
         * 默认链接
         * 
         * @var string 
         */
		var $_default_link = '';
        
        /**
         * 权限
         * 
         * @var array 
         */
		var $_rights = array();
        
        /**
         * 构造函数
         */
		function __construct() {
			$this->ci = &get_instance();
			$this->ci->settings->load('menus');//加载菜单数据
			$this->top_menus = & setting('menus');
			if($this->ci->_admin->role != 1) { //如果不是管理员模式，则加载相应的角色对应的文件
				$this->ci->settings->load('acl/role_'.$this->ci->_admin->role.EXT);//加载权限数据
				$this->top_menus = & setting('menus');
				$this->_rights = & setting('current_role');
			}
			$this->_filter_menus();
		}
		
        /**
         * 根据路由 http://url/admin/system/home
         * 获取控制器的类名和方法，进行菜单的选取
         */
		function _filter_menus() {
			$class_name = $this->ci->uri->rsegment(1);
			$method_name = $this->ci->uri->rsegment(2);
			switch($class_name) {
				case 'content' : 
				case 'category_content' : $this->_filter_content_menus($class_name,$method_name);break;
				case 'module' : $this->_filter_module_menus($class_name,$method_name);break;
				default : $this->_filter_normal_menus($class_name,$method_name);
			}
		}
		
        /**
         * 就是最基本的菜单，
         * 默认: 系统/内容管理/工具 中的 系统　项的操作
         * 
         * @param string $class_name 控制器
         * @param string $method_name 方法
         */
		function _filter_normal_menus($class_name,$method_name) {
			$this->_current_menu = 0; //当前菜单序号为0(相对于 系统/内容管理/工具 来说)
			$this->_default_link = backend_url('system/home');
			$this->left_menus = & $this->top_menus[$this->_current_menu]['sub_menus'];
			foreach($this->left_menus as $vkey => & $v) {
				foreach($v['sub_menus'] as $jkey => &$j) {
					if($j['class_name'] == $class_name && $j['method_name'] == $method_name) {
						$j['current'] = true;
					}
					if($this->ci->_admin->role == 1){continue;}
					$right = $j['class_name'].'@'.$j['method_name'];
					if(!in_array($right,$this->_rights['rights']) && $right !='system@home') {
						unset($this->left_menus[$vkey]['sub_menus'][$jkey]);	
					}
				} 
				if(!$v['sub_menus']){unset($this->left_menus[$vkey]);}  
			}
		}
		
        /**
         * 对于 内容模型 和 分类模型　的处理
         * 
         * @param string $class_name 控制器
         * @param string $method_name 方法
         */
		function _filter_content_menus($class_name,$method_name) {
			$this->_current_menu = 1; //当前菜单序号为1(相对于 系统/内容管理/工具 来说)
			$this->left_menus = & $this->top_menus[$this->_current_menu]['sub_menus'];
			$extra = $this->ci->input->get('model');
			foreach($this->left_menus as $vkey => & $v) {
				foreach($v['sub_menus'] as $jkey => &$j) {
					if($j['class_name'] == $class_name && $j['method_name'] == $method_name &&  ( ($j['extra'] == $extra && $vkey == 0) || ($j['extra'] == $extra && $vkey == 1) ) ) {
						$j['current'] = true;
					}
					
					if($this->ci->_admin->role == 1){continue;}
					$right = $j['class_name'].'@'.$j['method_name'];
					if(!in_array($right,$this->_rights['rights']) ||  (!in_array($j['extra'],$this->_rights['models']) && $vkey == 0) || (!in_array($j['extra'],$this->_rights['category_models']) && $vkey == 1) 		
					) {
						unset($this->left_menus[$vkey]['sub_menus'][$jkey]);
					}
				} 
				if(!$v['sub_menus']){unset($this->left_menus[$vkey]);}
			}
			//设定默认链接 
            $_item = @reset($this->left_menus[0]['sub_menus']);
			if($_item) {
			    if(!$this->_default_link) {
				    $this->_default_link = backend_url($_item['class_name'].'/view','model='.$_item['extra']);	
				}
			}
		}
		
        /**
         * 对于 工具 菜单的处理
         * 
         * @param string $class_name 控制器
         * @param string $method_name 方法
         */
		function _filter_module_menus($class_name,$method_name) { $this->_current_menu = 2;  } //当前菜单序号为2(相对于 系统/内容管理/工具 来说)
		
        /**
         * 检测插件菜单
         */
		function _detect_plugin_menus() {
			$this->top_menus[$this->_current_menu]['sub_menus'] = $this->ci->plugin_manager->trigger_left_menu();
			$this->left_menus = & $this->top_menus[$this->_current_menu]['sub_menus'];
			foreach($this->left_menus as $key=>&$v) {
				if(isset($v['sub_menus']) && $v['sub_menus']) { //如果有子菜单 
					foreach($v['sub_menus'] as &$j) {
						$j['extra'] = 'plugin='.$j['class_name'].'&action='.$j['method_name'];
						if($j['class_name'] == $this->ci->input->get('plugin') && $j['method_name'] == $this->ci->input->get('action')) {
							$j['current'] = true;
						}
						$j['class_name'] = 'module';
						$j['method_name'] = 'run';
						if(!$this->_default_link) {
							$this->_default_link = backend_url('module/run',$j['extra']);
						}
					}
				}
				else {
					unset($this->left_menus[$key]);	
				}
			}
		}
		
        /**
         * 权限检测
         * 
         * @param string $act 方法名称
         * @return bool 是否可访问 
         */
		function permit($act = '') {
			if($this->ci->_admin->role == 1) {
				return true;	
			}
			$class_method = $this->ci->uri->rsegment(1).'@'.$this->ci->uri->rsegment(2).($act ? '@'.$act : '');
			if(!in_array($class_method,$this->_rights['rights'])) { //如果在权限数据当中
				return false;	
			}
			if($this->ci->uri->rsegment(1) == 'content' ) { //如果属于内容模型管理部分
				if(!in_array($this->ci->input->get('model'),$this->_rights['models'])) {
					return false; 
				}
			}
			else if($this->ci->uri->rsegment(1) == 'category_content') { //如果属于分类模型管理部分 
				if(!in_array($this->ci->input->get('model'),$this->_rights['category_models'])) {
					return false; 
				}
			}
			else if($this->ci->uri->rsegment(1) == 'module')  { //如果属于模块(插件)管理部分
				if(!in_array($this->ci->input->get('plugin'),$this->_rights['plugins']))	 {		
					return false;
				}
			}
			return true;
		}
		
        /**
         * 显示顶部菜单
         */
		function show_top_menus() {
			foreach($this->top_menus as $key=>$v) { //在基类里面写输出语句，这样很不好
					echo '<li class="'. ($key == 0 ? 'first' : ($key == 3 ? 'last' : '')) .' '.($key == $this->_current_menu ? 'active' : '').'">
					  <a href="'.backend_url($v['class_name'].'/'.$v['method_name']).'">'.$v['menu_name'].'</a>
					  </li>';
			}
		}
		
        /**
         * 是示左部菜单
         */
		function show_left_menus() {
			foreach($this->left_menus as $v) {
				if($v['sub_menus']) {
					echo '<h4 class="leftback">'. $v['menu_name'] .'</h4>
						     <ul class="tabs">';
							 foreach($v['sub_menus'] as $j) {
							   $extra = '';
							   $this->_current_menu ==  1 && $extra =  'model='.$j['extra'] ;
							   $this->_current_menu ==  2 && $extra = $j['extra'];
							   echo '<li class="'.(isset($j['current']) ? 'active' : '').'"><a href="'.backend_url($j['class_name'].'/'.$j['method_name'],$extra).'">'.$j['menu_name'].'</a></li>';
							 }
					echo	 '</ul>';	
				}
			}
		}
	}